/**
 * 
 */   
package com.cares.infobird.service;  

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import com.cares.core.base.controller.BaseController;
import com.cares.core.base.service.BaseService;
import com.cares.entity.infobird.NoticeInfo;
import com.cares.utilities.email.MimeMailService;

/**   
 * @作者 Stone
 * @创建日期 2014-8-16 下午3:32:54
 * @版本 V1.0
 * @类说明
 */
@Component
public class NoticeDealService extends BaseService {
	@Resource(name="noticeInfoService")
	private NoticeInfoService noticeInfoService;
	
	@Autowired
	private MimeMailService mimeMailService;
	
	@Autowired
	private MessageSendService messageService;

	/**
	 * 消息推送
	 */
	public void dealNoticeInfo() {
		//获取待发送通知信息
		List<NoticeInfo> list = noticeInfoService.getNoticeList();
		if(list!=null) {
			for (NoticeInfo noticeInfo : list) {
				String channel = noticeInfo.getChannel();
				if("MOBILE".equals(channel)) {
					String content = noticeInfo.getContent();
					Map<String, String> sendInfo = messageService.sendSIMMsg(noticeInfo.getAddress(), content);

					if (!"Y".equals(sendInfo.get(BaseController.AJAX_MESSAGE))) {
						content += "[未发送：" + sendInfo.get(BaseController.ERR_MESSAGE) + "]";
						noticeInfo.setContent(content);
						noticeInfo.setSendState(2);
					} else {
						noticeInfo.setSendState(1);
					}
					noticeInfo.setSendFlag(1);
					noticeInfo.setSendTime(dateProvider.getDate());
					
					noticeInfoService.update(noticeInfo);
				} else if("EMAIL".equals(channel)) {
					int flag = mimeMailService.sendNoticeMail(noticeInfo);
					
					noticeInfo.setSendState(flag);
					noticeInfo.setSendFlag(1);
					noticeInfo.setSendTime(dateProvider.getDate());
					
					noticeInfoService.update(noticeInfo);
				}
			}
		}
		
	}
}
